package main.java.game.command;


import java.util.List;
import java.util.Random;

import main.java.game.Battle;
import main.java.game.Game;
import main.java.game.Player;
import main.java.game.combatant.Hero;
import main.java.game.combatant.Monster;
import main.java.game.combatant.MonsterMultipleTreasures;
import main.java.game.item.Treasure;
import main.java.game.item.equipableTreasure.EquipableTreasure;

public class StartBattle implements PreBattleCommand {

	
	private Monster monster;
	private Player player;
	private Hero hero;
	private Game game;
	
	
	public StartBattle (Game game,Monster monster, Player player, Hero hero){
		this.monster=monster;
		this.player=player;
		this.hero=hero;
		this.game=game;
	}
	public void execute() {
		Battle battle = new Battle(game,monster,player,hero);
		battle.fight();
		if(monster instanceof MonsterMultipleTreasures){
			if(battle.isDefeatedMonster())
					treasuresAssignement();
		}

		else if(battle.isDefeatedMonster() && monster.getLevelTreasure()!=0){
			treasureAssignement(monster.getLevelTreasure());
		}
	}
	
	private void treasuresAssignement(){
		MonsterMultipleTreasures mt=(MonsterMultipleTreasures)monster;
		Integer siz=mt.getLevels().size();
		for(int i=0; i<siz; i++){
			treasureAssignement(mt.getLevels().get(i));
		}
	}
	private void treasureAssignement(Integer level){
		Random random = new Random();
		List<Treasure> listT= game.getTreasures().get(level);
		Integer index = Math.abs(random.nextInt()%listT.size());
		Treasure treasure = listT.get(index);
		player.getTreasures().add(treasure);
		System.out.println("You got the treasure level "+ monster.getLevelTreasure()+" : "+treasure.getName());
	}
}

